package com.lanou.musicproject.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.SingerSongAdapter;
import com.lanou.musicproject.bean.Constant;
import com.lanou.musicproject.bean.MusicResult;
import com.lanou.musicproject.bean.MusicRoot;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/26.
 * 歌手歌曲页面
 */
public class SingerSongFragment extends Fragment {

    private long singerId;//用来接受歌手id用来网址解析
    private String url;
    private VolleySingleton volleySingleton;
    private ArrayList<MusicResult> data;
    //    private ListView listView;
    private RecyclerView listView;
    private SingerSongAdapter listAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View songView = inflater.inflate(R.layout.fragment_singer_song, null);
        listView = (RecyclerView) songView.findViewById(R.id.singer_song_list);

        return songView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 定义线性布局管理对象
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        // 设置列表方向
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        // 设置布局管理器到recylerview
        listView.setLayoutManager(manager);


        Bundle bundle = getArguments();
        singerId = bundle.getLong("singer");
        url = Constant.SINGER_INFOR + singerId + Constant.SINGER_SONGS_END;
        getData(url);
    }

    private void getData(final String url) {
        volleySingleton = VolleySingleton.getInstance(getActivity());
        JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.e("sdasd",url);
                String string = response.toString();
                Gson gson = new Gson();
                MusicRoot root = gson.fromJson(string, MusicRoot.class);
                data = root.getData();
                listAdapter = new SingerSongAdapter(getActivity(), data);
                listView.setAdapter(listAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

}
